/**
 * Coryright 2015-2020 www.eyaomall.cn
 * All rights reserved.
 * @author huwenqing
 * @date 2016年10月18日
 *
 */

package com.eyaomall.wxmall.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.dao.CouponDao;
import com.eyaomall.wxmall.model.po.Coupon;
import com.eyaomall.wxmall.model.po.CouponGoods;
import com.eyaomall.wxmall.model.po.CouponMember;
import com.eyaomall.wxmall.model.vo.CouponVO;
import com.eyaomall.wxmall.service.CouponService;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;
import com.lycheeframework.core.cmp.kit.Pages;
import com.lycheeframework.core.common.util.DateUtil;
import com.lycheeframework.core.exception.ErrorCodeException;

@Service
public class CouponServiceImpl implements CouponService {

	@Autowired
	private CouponDao couponDao;

	/**
	 * 优惠券数据
	 */
	public Coupon queryDetail(Coupon coupon){
		
		return (Coupon) couponDao.query("queryCoupon",coupon);
	}

	/**
	 * 优惠券列表数据
	 */
	public Pages<?> page(Coupon coupon, PageHTTPObject pageHTTPObject) {
		coupon.setShopId(MemberHelper.getShopId());

		return couponDao.page(coupon, pageHTTPObject);
	}

	/**
	 * 根据优惠券查询我的优惠券详情
	 */
	public Coupon queryMyCouponDetail(Integer couponId) {

		return couponDao.queryMyCouponDetail(couponId);
	}

	/**
	 * 查询我领取的优惠券列表
	 */
	public List<Coupon> listMyCoupons(CouponVO couponVO) {

		return couponDao.listMyCoupon(couponVO);
	}
	
	/**
	 * 根据优惠券ID查询我的优惠券详情
	 * 
	 * @param couponId
	 * @return
	 */
	public Coupon queryMyGetDetail(Integer couponId) {
		
		return couponDao.queryMyGetDetail(couponId);
	}
	
	/**
	 * 查询我领取的优惠券列表
	 */
	public List<CouponGoods> listCouponGoods(CouponVO couponVO) {

		return couponDao.listCouponGoods(couponVO);
	}

	/**
	 * 查询我的优惠券产品关联
	 * 
	 * @param couponId
	 * @return
	 */
	public CouponGoods queryCouponGoods(Integer couponId) {

		return (CouponGoods) couponDao.query("queryByGoods", couponId);
	}

	/**
	 * 新增优惠券会员信息
	 * 
	 * @param couponMember
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public int createCouponMember(Integer couponId) {
		Coupon coupon = couponDao.queryById(couponId);
		if (coupon == null)
			throw new ErrorCodeException(BECode.coupon_8100000);
		
		if(!"0".equalsIgnoreCase(coupon.getStatus()))
			throw new ErrorCodeException(BECode.coupon_8100006);

		// 校验优惠券是否过期
		if (coupon.getGmtEnd().compareTo(DateUtil.parse(DateUtil.formatDateTime(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd")) < 0)
			throw new ErrorCodeException(BECode.coupon_8100005);

		// 判断已领取的数据是否已经等于发行量（校验是否还可以领取）
		if (coupon.getAlreadyEmploy() >= coupon.getNum())
			throw new ErrorCodeException(BECode.coupon_8100004);

		// 更新已经领取的数量
		coupon.setAlreadyGet(coupon.getAlreadyGet() + 1);
		
		if(coupon.getAlreadyGet() >= coupon.getNum()){
			coupon.setStatus("2");
		}
		couponDao.update(coupon);
		
		CouponMember member = new CouponMember();
		member.setCouponId(couponId);
		member.setMemberId(MemberHelper.getId());
		member = (CouponMember) couponDao.query("queryCouponMemeber", member);

		if(member != null)
			throw new ErrorCodeException(BECode.coupon_8100004);
		
		member = new CouponMember();
		member.setCouponId(couponId);
		member.setMemberId(MemberHelper.getId());
		return couponDao.addCouponMember(member);
	}

	/**
	 * 修改我的优惠券会员关系
	 * 
	 * @param couponId
	 * @param memberId
	 * @param status
	 * @return
	 */
	public boolean updateCouponMember(Integer couponId, Integer memberId, String status) {

		return couponDao.updateCouponMember(couponId, memberId, status);
	}
	
	/**
	 * 根据邮回去ID查询我的优惠券产品关系
	 * 
	 * @return
	 */
	public List<CouponGoods> listGoodsById(CouponVO couponVO){
		
		return couponDao.listGoodsById(couponVO);
	}

	/**
	 * 修改优惠券信息
	 * 
	 * @param coupon
	 * @return
	 */
	public boolean update(Coupon coupon) {

		return couponDao.update(coupon) > 0 ? true : false;
	}
}
